package com.hdu.hot100.tree;




public class lc124 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        int ans = Integer.MIN_VALUE;
        public int maxPathSum(TreeNode root)
        {
            f(root);
            return ans;
        }

        int f(TreeNode root)
        {
            if (root == null)
            {
                return 0;
            }
            int left = Math.max(f(root.left), 0);
            int right = Math.max(f(root.right), 0);
            ans = Math.max(ans, left + right + root.val);
            return Math.max(left, right) + root.val;
        }
    }
}
